package com.seleniti.enpareja.android.ui.widget;

import org.ioc.commons.impl.android.ui.SavedState;
import org.ioc.commons.ui.HasValue;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.seleniti.enpareja.android.R;

public class PendingMark extends LinearLayout implements HasValue<Integer> {

	private Integer value;
	private TextView label;

	public PendingMark(Context context) {
		super(context);
		init();
	}

	public PendingMark(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public PendingMark(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	private void init() {
		this.label = new TextView(getContext());
		this.addView(this.label, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		this.label.setBackgroundResource(R.drawable.pending_counter);
		this.label.setTextColor(Color.parseColor("#FFFFFF"));
		this.label.setGravity(Gravity.CENTER);
		this.label.setTypeface(null, Typeface.BOLD);
		this.setVisibility(View.GONE);
	}

	@Override
	protected Parcelable onSaveInstanceState() {
		Parcelable superState = super.onSaveInstanceState();
		SavedState ss = new SavedState(superState);

		// TODO: Ampliar el estado para meter textos
		ss.setStateToSave(this.value != null && this.value > -1 ? this.value : -1);

		return ss;
	}

	@Override
	public void onRestoreInstanceState(Parcelable state) {
		// begin boilerplate code so parent classes can restore state
		if (!(state instanceof SavedState)) {
			super.onRestoreInstanceState(state);
			return;
		}

		SavedState ss = (SavedState) state;
		super.onRestoreInstanceState(ss.getSuperState());

		int value = ss.getStateToSave();

		this.setValue(value > -1 ? value : null);

	}

	@Override
	public void setValue(Integer value) {
		this.setVisibility(value != null && value > 0 ? View.VISIBLE : View.GONE);
		this.value = value;
		this.label.setText(value != null ? value.toString() : "");
	}

	@Override
	public Integer getValue() {
		return this.value;
	}

}
